import hashlib
import os.path

from .node import Node


class FileNode(Node):
    def __init__(self,
                 filename):
        if not os.path.isabs(filename):
            raise ValueError(
                'FileNode.filename must be an absolute path. \
                 (filename={})'.format(
                    filename))

        super(FileNode, self).__init__()

        self.filename = filename

    def execute(self):
        return True

    @property
    def signature(self):
        # TODO: Cache the return value?

        m = hashlib.md5()

        with open(self.filename, 'r') as f:
            m.update(f.read().encode('utf-8'))

        return m.digest()

    @property
    def id(self):
        return self.filename
